package com.dushibao.dao;

import cn.hutool.core.util.ObjectUtil;
import com.dushibao.common.Constants;
import com.dushibao.entity.Address;
import com.dushibao.utils.DBUtils;
import com.dushibao.utils.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AddressDao {


    public Integer save(Address address) {

        try{
            String sql = "insert into dushibao_address(area_info,mobile,telephone,trueName,zip,user_id) values(?,?,?,?,?,?)";

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,address.getAreaInfo());
            stmt.setObject(2,address.getMobile());
            stmt.setObject(3,address.getTelephone());
            stmt.setObject(4,address.getTrueName());
            stmt.setObject(5,address.getZip());
            stmt.setObject(6,address.getUserId());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    public int update(Address address)  {
        try{
            String sql = "update dushibao_address set area_info=?,mobile=?,telephone=?,trueName=?,zip=?,user_id=? where id=?";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,address.getAreaInfo());
            stmt.setObject(2,address.getMobile());
            stmt.setObject(3,address.getTelephone());
            stmt.setObject(4,address.getTrueName());
            stmt.setObject(5,address.getZip());
            stmt.setObject(6,address.getUserId());

            stmt.setLong(7,address.getId());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public Page page(Page page , Map<String,Object> params) {

        try{
            StringBuilder querySql = new StringBuilder("select id,addTime,area_info,mobile,telephone,trueName,zip,user_id from dushibao_address where 1=1 ");

            if(ObjectUtil.isNotEmpty(params.get("startTime"))){
                querySql.append(" and addTime > '"+params.get("startTime")+"'");
            }

            if(ObjectUtil.isNotEmpty(params.get("endTime"))){
                querySql.append(" and addTime < '"+params.get("endTime")+"'");
            }

            querySql.append(" limit ?,? ");

            Connection conn = DBUtils.getConnection();

            PreparedStatement stmt = conn.prepareStatement(querySql.toString());
            stmt.setInt(1,page.getStart());
            stmt.setInt(2,page.getPageSize());

            ResultSet rs = stmt.executeQuery();

            List<Object> objs = new ArrayList<>();
            while(rs.next()){
                Address address = new Address();
                address.setId(rs.getLong("id"));
                address.setAddTime(rs.getTimestamp("addTime").toLocalDateTime());
                address.setAreaInfo(rs.getString("area_info"));
                address.setMobile(rs.getString("mobile"));
                address.setTelephone(rs.getString("telephone"));
                address.setTrueName(rs.getString("trueName"));
                address.setZip(rs.getString("zip"));
                address.setUserId(rs.getLong("user_id"));
                objs.add(address);
            }

            DBUtils.close(rs,stmt,conn);

            StringBuilder countSql = new StringBuilder("select count(*) as total from dushibao_address where 1=1 ");

            if(ObjectUtil.isNotEmpty(params.get("startTime"))){
                countSql.append(" and addTime > '"+params.get("startTime")+"'");
            }

            if(ObjectUtil.isNotEmpty(params.get("endTime"))){
                countSql.append(" and addTime < '"+params.get("endTime")+"'");
            }

            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(countSql.toString());

            rs = stmt.executeQuery();

            int total = 0;
            if(rs.next()){
                total = rs.getInt("total");
            }

            Page iPage = new Page();
            iPage.setCurrent(page.getCurrent());
            iPage.setPageSize(page.getPageSize());
            iPage.setData(objs);
            iPage.setPages(total % iPage.getPageSize()==0?total/ iPage.getPageSize():total/ iPage.getPageSize()+1);
            iPage.setTotal(total);

            return iPage;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public Address getById(long id) {
        try{
            String sql = "select id,addTime,area_info,mobile,telephone,trueName,zip,user_id from dushibao_address where id=? ";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setLong(1,id);
            ResultSet rs = stmt.executeQuery();
            Address address = new Address();

            if(rs.next()){
                address.setId(rs.getLong("id"));
                address.setAddTime(rs.getTimestamp("addTime").toLocalDateTime());
                address.setAreaInfo(rs.getString("area_info"));
                address.setMobile(rs.getString("mobile"));
                address.setTelephone(rs.getString("telephone"));
                address.setTrueName(rs.getString("trueName"));
                address.setZip(rs.getString("zip"));
                address.setUserId(rs.getLong("user_id"));
            }

            return address;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    public Integer deleteById(List<Long> ids) {
        try{

            StringBuilder sql = new StringBuilder(" delete from dushibao_address where id in ( ");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));

                if(i<ids.size()-1){
                    sql.append(",");
                }

            }
            sql.append(")");

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
